package com.dylan.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.builder.xml.dynamic.TrimSqlNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dylan.entity.User;
import com.dylan.service.UserServiceImp;
import com.dylan.util.StringUtil;

/**
 * 用户视图控制器类
 * 
 * @author dylanchan
 *
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private UserServiceImp userServiceImp;

	public static final int PAGESIZE = 10;

	@RequestMapping(value = "/userList")
	public ModelAndView userList(HttpServletRequest request) {
		// 下一页页码
		int nextPageNum;
		int totalRows;
		int totalPageNum;
		int startNum;  //开始行
		
		String userName;
		ModelAndView mv = new ModelAndView();
		// 判断是否有传入页码
		if (StringUtil.isNotEmpty(request.getParameter("pageNum"))) {
			nextPageNum = Integer.parseInt(request.getParameter("pageNum"));
		} else {
			nextPageNum = 1;
		}

		// 获取页面传过来的查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(request.getParameter("userName"))){
			userName = request.getParameter("userName");
			paramMap.put("userName", userName);
			mv.addObject("userName", userName);
		}else{
			paramMap.put("userName", "");
			mv.addObject("userName", "");
		}
		// 查询总记录数
		totalRows = userServiceImp.getAllUserCount(paramMap);
		// 计算总页数
		totalPageNum = (totalRows - 1) / PAGESIZE + 1;
		// 计算开始行数
		startNum = (nextPageNum - 1) * PAGESIZE + 1;
		// List<User> users = userServiceImp.getAllUser();
		paramMap.put("pageSize", PAGESIZE);
		paramMap.put("startNum", startNum);
		List<User> users = userServiceImp.getUsersByPage(paramMap);
		
		mv.addObject("users", users);
		mv.addObject("currentPageNum", nextPageNum);
		mv.addObject("totalPageNum", totalPageNum);
		mv.setViewName("/user/userList");
		return mv;
	}

	/**
	 * 模糊查询
	 * 
	 * @param request
	 * @return
	 */
//	@RequestMapping(value = "/userSearch")
//	public ModelAndView search(HttpServletRequest request) {
//		String userName = request.getParameter("userName");
//		Map<String, Object> searchParamMap = new HashMap<String, Object>();
//		searchParamMap.put("userName", userName);
//		ModelAndView mv = new ModelAndView();
//		mv.addObject("searchParamMap", searchParamMap);
//
//		mv.setViewName("/user/userList");
//		return mv;
//	}

	@RequestMapping(value = "/userAdd")
	public ModelAndView addUser(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		ModelAndView mv = new ModelAndView();
		if (!(userId == null || "".equals(userId.trim()))) {
			mv.addObject("user", userServiceImp.getUserById(Integer.parseInt(userId)));
		}
		mv.setViewName("/user/userAdd");
		return mv;
	}

	// @RequestMapping(value="/userUpdate")
	// public ModelAndView updateUser(){
	// ModelAndView mv = new ModelAndView();
	// mv.setViewName("/user/userUpdate");
	// return mv;
	// }

	@RequestMapping(value = "/userSubmit")
	public String saveOrUpdate(HttpServletRequest request) {
		User user = new User();
		user.setname(request.getParameter("userame"));
		user.setAge(Integer.parseInt(request.getParameter("age")));

		// 判断是新增还是修改
		if (StringUtil.isNotEmpty(request.getParameter("userId"))) {
			System.out.println(request.getParameter("userId"));
			user.setId(Integer.parseInt(request.getParameter("userId")));
			userServiceImp.updateUser(user);
		} else {
			userServiceImp.saveUser(user);
		}

		// 新增或修改后返回用户列表
		return "redirect:/user/userList.do";
	}

	@RequestMapping(value = "/userDelete")
	public String delete(HttpServletRequest request) {
		// 判断是新增还是修改
		String userId = request.getParameter("userId");
		if (!(userId == null || "".equals(userId.trim()))) {
			userServiceImp.deleteUser(Integer.parseInt(userId));
		}

		// 新增或修改后返回用户列表
		return "redirect:/user/userList.do";
	}

}
